<?php

use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use common\models\Category;
use admin\models\Book;
use metronic\widgets\ActiveForm;
if (Yii::$app->session->getFlash('updated')) {
    \metronic\assets\ToastrAsset::register($this);
    $this->registerJs('toastr.success("保存成功", "", {timeOut: 1000});');
}

$this->registerJs('$(".field-book-flag").on("change", ":checkbox", function() {
        var $this = $(this);
        var value = $this.val();

        $("#book-flag").val(function(index, flag) {
            if ($this.prop("checked")) {
                return flag | value;
            } else {
                return flag & ~value;
            }
        });
    });');
?>

<div class="book-form">

    <?php $form = ActiveForm::begin() ?>

    <?= $form->field($model, 'channel_id')->dropDownList(Category::$channels, ['id' => 'channel_id'])->wrapper(['width' => 2]) ?>

    <div class="form-group field-book-cid1 required">
        <label class="col-md-3 control-label" for="book-cid1">所属分类<span class="required" aria-required="true"> * </span></label>
        <div class="col-md-4">
            <div style="width:48%; float:left">
                <?= Html::activeDropDownList($model, 'cat', ArrayHelper::map(Category::findAll(['pid' => 0]), 'id', 'title', 'channel_id'), ['id' => 'cat', 'class' => 'form-control', 'prompt' => '请选择']) ?>
                <div class="help-block"></div>
            </div>
        </div>
    </div>

    <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'old_name')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'source')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'author')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'description')->textarea(['rows' => 7])->wrapper(['width' => 7]) ?>

    <?= $form->field($model, 'cover')->imageUpload(['width' => 150, 'height' => 200]) ?>

    <div class="form-group field-book-flag">
        <label class="col-md-3 control-label" for="book-flag">属性</label>
        <div class="col-md-9">
            <?= Html::activeHiddenInput($model, 'flag') ?>
            <?php foreach ($model::$flags as $value => $label): ?>
                <?php if ($value > 2): ?>
                    <label class="checkbox-inline">
                        <input type="checkbox" value="<?= $value ?>" <?= $model->flag & $value ? 'checked="checked"' : '' ?>> <?= $label ?>
                    </label>
                <?php endif ?>
            <?php endforeach ?>
        </div>
    </div>

    <?= $form->field($model, 'status')->dropDownList(Book::$statuses)->wrapper(['width' => 2]) ?>

    <?php ActiveForm::end() ?>

</div>
